package com.example.demo.controller;

import com.example.demo.beans.HttpResponseEntity;
import com.example.demo.dao.entity.AnswerSheetEntity;
import com.example.demo.service.AnswerSheetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping
public class AnswerSheetController {
    @Autowired
    private AnswerSheetService answerSheetService;

    @RequestMapping(value="/searchAS",method= RequestMethod.POST,headers = "Accept=application/json")
    public HttpResponseEntity search(@RequestBody AnswerSheetEntity answerSheetEntity){
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
        try{
            List<AnswerSheetEntity> hasAnswerSheet = answerSheetService.search(answerSheetEntity);

            if(CollectionUtils.isEmpty(hasAnswerSheet)){
                httpResponseEntity.setCode("0");
                httpResponseEntity.setData(null);
                httpResponseEntity.setMessage("无答卷信息");
            }else{
                httpResponseEntity.setCode("666");
                httpResponseEntity.setData(hasAnswerSheet);
                httpResponseEntity.setMessage("查询成功");
            }
        }catch(Exception e){
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        return httpResponseEntity;
    }

    @RequestMapping(value="/searchASByRespondentName",method= RequestMethod.POST,headers = "Accept=application/json")
    public HttpResponseEntity searchByRespondentName(@RequestBody AnswerSheetEntity answerSheetEntity){
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
        try{
            List<AnswerSheetEntity> hasAnswerSheet = answerSheetService.searchByRespondentName(answerSheetEntity);

            if(CollectionUtils.isEmpty(hasAnswerSheet)){
                httpResponseEntity.setCode("0");
                httpResponseEntity.setData(null);
                httpResponseEntity.setMessage("无答卷信息");
            }else{
                httpResponseEntity.setCode("666");
                httpResponseEntity.setData(hasAnswerSheet);
                httpResponseEntity.setMessage("查询成功");
            }
        }catch(Exception e){
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        return httpResponseEntity;
    }

    @RequestMapping(value="/searchASByQuestionnaireName",method= RequestMethod.POST,headers = "Accept=application/json")
    public HttpResponseEntity searchByQuestionnaireName(@RequestBody AnswerSheetEntity answerSheetEntity){
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
        try{
            List<AnswerSheetEntity> hasAnswerSheet = answerSheetService.searchByQuestionnaireName(answerSheetEntity);

            if(CollectionUtils.isEmpty(hasAnswerSheet)){
                httpResponseEntity.setCode("0");
                httpResponseEntity.setData(null);
                httpResponseEntity.setMessage("无答卷信息");
            }else{
                httpResponseEntity.setCode("666");
                httpResponseEntity.setData(hasAnswerSheet);
                httpResponseEntity.setMessage("查询成功");
            }
        }catch(Exception e){
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        return httpResponseEntity;
    }
}
